from flask import Blueprint, jsonify, request, json
import pymysql as sql

form = Blueprint('form', __name__)

connect = sql.Connect(
    host='bj-cdb-hjbj4oh0.sql.tencentcdb.com',
    port=60670,
    user='root',
    passwd='Yuan2000_',
    db='tencentmysql',
    charset='utf8'
)
cursor = connect.cursor()


@form.route('/formInfo', methods=['GET'])
def employeeInfo():
    sql = "SELECT id, event_type, event_date, event_location, event_desc FROM event_info "
    cursor.execute(sql)

    attribute = ['id', 'event_type', 'event_date', 'event_location', 'event_desc']
    l = []
    for item in cursor.fetchall():
        dic = dict(map(lambda x, y: [x, y], attribute, item))
        l.append(dic)
    # print(l)
    return jsonify(l)


@form.route('/userName', methods=['GET'])
def userName():
    sql = "SELECT UserName FROM sys_user "
    cursor.execute(sql)

    attribute = ['UserName']
    l = []
    for item in cursor.fetchall():
        dic = dict(map(lambda x, y: [x, y], attribute, item))
        l.append(dic)
    # print(l)
    return jsonify(l)


@form.route('/text', methods=['POST'])
def newemployee():
    print("new a newemployee")
    data = request.get_data()
    print(data)
    json_data = json.loads(data.decode("utf-8"))
    print(json_data)
    text = json_data.get('imgbase')
    id = json_data.get('id')
    sql = "update `employee_info` set `imgset_dir`='" + text + "' where (`id`='" + id + "')"
    print(sql)
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        return jsonify(2)
    else:
        return jsonify(1)


@form.route('/text1', methods=['POST'])
def newemployee1():
    data = request.get_data()
    json_data = json.loads(data.decode("utf-8"))
    id = json_data.get('id')
    sql = "SELECT imgset_dir FROM employee_info where id = '" + id + "' "
    cursor.execute(sql)

    attribute = ['imgset_dir']
    l = []
    for item in cursor.fetchall():
        dic = dict(map(lambda x, y: [x, y], attribute, item))
        l.append(dic)
    print(l)
    return jsonify(l)

@form.route('/1text', methods=['POST'])
def newemployee_1():
    print("new a newemployee")
    data = request.get_data()
    print(data)
    json_data = json.loads(data.decode("utf-8"))
    print(json_data)
    text = json_data.get('imgbase')
    id = json_data.get('id')
    sql = "update `volunteer_info` set `imgset_dir`='" + text + "' where (`id`='" + id + "')"
    print(sql)
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        return jsonify(2)
    else:
        return jsonify(1)


@form.route('/1text1', methods=['POST'])
def newemployee1_1():
    data = request.get_data()
    json_data = json.loads(data.decode("utf-8"))
    id = json_data.get('id')
    sql = "SELECT imgset_dir FROM volunteer_info where id = '" + id + "' "
    cursor.execute(sql)

    attribute = ['imgset_dir']
    l = []
    for item in cursor.fetchall():
        dic = dict(map(lambda x, y: [x, y], attribute, item))
        l.append(dic)
    print(l)
    return jsonify(l)

@form.route('/2text', methods=['POST'])
def newemployee_1():
    print("new a newemployee")
    data = request.get_data()
    print(data)
    json_data = json.loads(data.decode("utf-8"))
    print(json_data)
    text = json_data.get('imgbase')
    id = json_data.get('id')
    sql = "update `oldperson_info` set `imgset_dir`='" + text + "' where (`id`='" + id + "')"
    print(sql)
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        return jsonify(2)
    else:
        return jsonify(1)


@form.route('/2text1', methods=['POST'])
def newemployee1_1():
    data = request.get_data()
    json_data = json.loads(data.decode("utf-8"))
    id = json_data.get('id')
    sql = "SELECT imgset_dir FROM oldperson_info where id = '" + id + "' "
    cursor.execute(sql)

    attribute = ['imgset_dir']
    l = []
    for item in cursor.fetchall():
        dic = dict(map(lambda x, y: [x, y], attribute, item))
        l.append(dic)
    print(l)
    return jsonify(l)